package com.njym.java.security.app.rest;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.njym.java.security.app.biz.PostCommentBiz;
import com.njym.java.security.app.entity.PostComment;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.msg.TableResultResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("postComment")
public class PostCommentController extends BaseController<PostCommentBiz, PostComment> {
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Object list(String postId) {
		if (!postId.equals("undefined")) {
			Example example = new Example(PostComment.class);
			example.createCriteria().andEqualTo("postId",Integer.parseInt(postId)).andEqualTo("deleted",0);
			example.orderBy("dateline");
			List<PostComment> list = baseBiz.selectByExample(example);
			//筛选出第一楼的评论
			List<Map> bigList = new ArrayList();
			List<PostComment> firstFloor = list.stream().filter(m->m.getPid().equals(0)).collect(Collectors.toList());
			//第二楼的回复
			for (PostComment postComment: firstFloor){
				Map<String,Object> hashMap = new HashMap<>(2);
				List<PostComment> secondFloor = list.stream().filter(s->s.getPid().equals(postComment.getId())).collect(Collectors.toList());
				hashMap.put("comment",postComment);
				if(secondFloor.size()>0) hashMap.put("replys",secondFloor);
				bigList.add(hashMap);
			}
			return new ObjectRestResponse<List>().data(bigList);
		} else {
			return ResponseUtil.fail(40301, "错误！");
		}
	}

	/**
	 * 查询我的帖子-评论
	 * @param limit
	 * @param page
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/comment", method = RequestMethod.GET)
	@ResponseBody
	public Object comment(@RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1")int page,
						  String userId){
		if (!userId.equals("undefined")) {
			Page<Object> result = PageHelper.startPage(page, limit);
			baseBiz.comment(Integer.parseInt(userId));
			return new TableResultResponse<>(result.getPages(),result.getResult());
		} else {
			return ResponseUtil.fail(40301, "错误！");
		}
	}

	/**
	 * 查询我的帖子-回复
	 * @param limit
	 * @param page
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.GET)
	@ResponseBody
	public Object reply(@RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1")int page,
						  String userId){
		if (!userId.equals("undefined")) {
			Page<Object> result = PageHelper.startPage(page, limit);
			baseBiz.reply(Integer.parseInt(userId));
			return new TableResultResponse<>(result.getPages(),result.getResult());
		} else {
			return ResponseUtil.fail(40301, "错误！");
		}
	}
}